package com.it.mybatisplus.springbootmybatisplu.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.it.mybatisplus.springbootmybatisplu.beans.bo.TestBO;
import com.it.mybatisplus.springbootmybatisplu.beans.bo.UserBO;
import com.it.mybatisplus.springbootmybatisplu.model.Test;
import com.it.mybatisplus.springbootmybatisplu.model.User;
import com.it.mybatisplus.springbootmybatisplu.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 通过mybatis2.3x，自定义sql来执行
 */
@RestController
@RequestMapping("/userDefinedController")
public class UserDefinedController {
    @Autowired
    private UserService userService;
    @RequestMapping("/addUser")
    public String addTest(@RequestBody UserBO  userBO){
        User user = new User();
        BeanUtils.copyProperties(userBO,user);
        userService.addUser(user);
        return "ok";
    }
    @RequestMapping("/delUser")
    public String delTest(@RequestBody UserBO  userBO){
        User user = new User();
        BeanUtils.copyProperties(userBO,user);
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.eq("id",user.getId());
        userService.delUser(user);
        return "ok";
    }
    @RequestMapping("/queryUser")
    public String queryTest(@RequestBody UserBO  userBO){
        User user = new User();
        BeanUtils.copyProperties(userBO,user);

        user =userService.queryUser(user);
        return JSONUtil.parse(user).toString();
    }
    @RequestMapping("/updateUser")
    public String updateTest(@RequestBody UserBO  userBO){
        User user = new User();
        BeanUtils.copyProperties(userBO,user);

        userService.updateUser(user);
        return "ok";
    }

}
